{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test GWDLR to Centerlines\n",
    "\n",
    "**GWDLR**: This is an optional package to convert Global Width\n",
    "   Database-Large Rivers raster data provided by\n",
    "   [Dai Yamazaki](mailto:bigasmountain1022@gmail.com)  to vectors that can be used as\n",
    "   centerlines. This part tests going to a thinned geotiff file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from os.path import exists\n",
    "def find_riverobs_test_data_dir():\n",
    "    \"\"\"Fin the location of the test data root directory\"\"\"\n",
    "    \n",
    "    if 'RIVEROBS_TESTDATA_DIR' in os.environ:\n",
    "        test_data_dir = os.environ('RIVEROBS_TESTDATA_DIR')\n",
    "    else: # try the default location\n",
    "        test_data_dir = '../../../RiverObsTestData'\n",
    "        \n",
    "    if not exists(test_data_dir):\n",
    "        print('You must either set the environment variable RIVEROBS_TESTDATA_DIR')\n",
    "        print('or locate the test data directory at ../../../RiverObsTestData')\n",
    "        raise Exception('Test data directory not found.')\n",
    "        \n",
    "    return test_data_dir\n",
    "\n",
    "data_dir = find_riverobs_test_data_dir()\n",
    "data_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from os.path import join\n",
    "from GWDLR import GWDLR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data_dir = join(data_dir,'GWDLR')\n",
    "root_name = 'n35w125_wth'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gwdlr = GWDLR(root_name,data_dir)\n",
    "gwdlr.__dict__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Thin the data to get centerlines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "min_width = 25.\n",
    "gwdlr.to_mask(min_width,overwrite=True,thin=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "figsize(8,8)\n",
    "imshow(gwdlr.data,cmap=cm.gray)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output the thinned center line data to a Geotiff file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mask_file = join(data_dir,root_name+'_center_line_%d.tif'%min_width)\n",
    "gwdlr.to_gdal(mask_file,gdal_format='GTiff')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
